﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookExchange
{
    public partial class FileUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var fileDownloadInfos = Directory
                .GetFiles(this.Folder).ToList()
                .Select(item => new FileDownloadInfo(new FileInfo(item))).ToList();

            fileDownloadInfos.ForEach(item => item.EncodedName = Server.UrlEncode(item.Name));


            Repeater1.DataSource = fileDownloadInfos;
            Repeater1.DataBind();

        }

        string Folder
        {
            get
            {
                return Server.MapPath("Files");
            }
        }

        protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            string filename = Path.Combine(Folder, e.FileName);
            AsyncFileUpload1.SaveAs(filename);
            txtFilename.InnerText = e.FileName;

            Response.Redirect("Default.aspx");
        }
    }

    public class FileDownloadInfo
    {

        public FileDownloadInfo(FileInfo fileInfo)
            : this(fileInfo.Name, fileInfo.Length, fileInfo.CreationTime)
        {

        }

        public FileDownloadInfo(string fileName, long fileSize, DateTime uploadDate)
        {
            this.Name = fileName;
            this.Size = fileSize;
            this.Date = uploadDate.ToString();
        }


        public string Date { get; set; }
        public long Size { get; set; }
        public string EncodedName { get; set; }
        public string Name { get; set; }
        public string DownloadUrl
        {
            get
            {
                return "http://bookexchange.travianoverview.com/Files/" + this.Name;
            }
        }
    }
}